package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.mytools.tool.utils.SharedUtil;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity implements OnPageChangeListener {
	
	private ViewPager view_pager;
	private int[] imgs = {R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3, R.mipmap.guide4 };
	private ArrayList<View> views;
	private ViewGroup scrollDot;
	private int index;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		isImmersiveNotification = true;
		setContentView(R.layout.activity_guide);
		super.onCreate(savedInstanceState);
		initview();
	}
	
	private void initview(){
		view_pager = (ViewPager) this.findViewById(R.id.view_pager);
		initViewPager();
	}
	
	private void initViewPager() {
		views = loadViews(imgs);
		view_pager = (ViewPager) this.findViewById(R.id.view_pager);
		scrollDot = (ViewGroup) this.findViewById(R.id.scroll_dot);
		view_pager.setVisibility(View.VISIBLE);
		((ImageView) scrollDot.getChildAt(0)).setEnabled(false);
		view_pager.setOnPageChangeListener(this);
		view_pager.setAdapter(new PagerAdapter() {

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}

			@Override
			public int getCount() {return views.size();}

			public Object instantiateItem(ViewGroup container, int position) {
				if (position == 3) {
					views.get(position).setOnClickListener(
							new OnClickListener() {
								@Override
								public void onClick(View v) {
									//加上PageTransformer
									if (index == views.size() - 1) {
										mApp.shareUtil.setShareBooleanPara(SharedUtil.SHARE_OPEN_FIRSTTIME, false);
										Intent i = new Intent(getApplicationContext(), LoginActivity.class);
										startActivity(i);
										finish();
									}
								}
							});
				}
				((ViewPager) container).addView(views.get(position));
				return views.get(position);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				((ViewPager) container).removeView(views.get(position));
			}
		});
	}

	public ArrayList<View> loadViews(int[] mLoadPage) {
		ArrayList<View> views = new ArrayList<View>();
		int length = mLoadPage.length;
		for (int index = 0; index < length; index++) {
			RelativeLayout layout = new RelativeLayout(GuideActivity.this);
			layout.setBackgroundResource(mLoadPage[index]);
			views.add(layout);
		}
		return views;
	}

	//一下三个方法为 OnPageChangeListener的借口方法
	@Override
	public void onPageScrollStateChanged(int arg0) {}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {}
	@Override
	public void onPageSelected(int position) {
		index = position;
		if (position == 0) {
			((ImageView) scrollDot.getChildAt(0)).setEnabled(false);
			((ImageView) scrollDot.getChildAt(1)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(2)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(3)).setEnabled(true);
		} else if (position == 1) {
			((ImageView) scrollDot.getChildAt(0)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(1)).setEnabled(false);
			((ImageView) scrollDot.getChildAt(2)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(3)).setEnabled(true);
		} else if (position == 2) {
			((ImageView) scrollDot.getChildAt(0)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(1)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(2)).setEnabled(false);
			((ImageView) scrollDot.getChildAt(3)).setEnabled(true);
		} else if (position == 3) {
			((ImageView) scrollDot.getChildAt(0)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(1)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(2)).setEnabled(true);
			((ImageView) scrollDot.getChildAt(3)).setEnabled(false);
		}
	}
	

}
